import * as THREE from "three";
import { reactive, toRaw } from "vue";

export const playerState: {
  playerMesh: any; //玩家几何对象
  playerBody: any; //玩家刚体对象
  direction: any; //当前玩家移动方向
  isCrash: boolean;
  isMove: boolean; //当前刚体是否正在移动
} = reactive({
  playerMesh: null,
  playerBody: null,
  direction: null,
  isCrash: false,
  isMove: false,
});

export class Player {
  scene: any;
  camera: any;
  height: number = 1; //胶囊高度
  radius: number = 0.25;//胶囊半径
  speed: number = 0.2;
  type: number = 1; //1第一人称，2第三人称

  constructor(scene: any, camera: any) {
    this.scene = scene;
    this.camera = camera;
    this.init();
  }

  init() {
    playerState.playerMesh = new THREE.Mesh(
      new THREE.CapsuleGeometry(this.radius, this.height),
      new THREE.MeshNormalMaterial()
    );
    playerState.playerMesh.name = "player";
    playerState.playerMesh.rotation.order = "YXZ";
    this.scene.add(toRaw(playerState.playerMesh));
    this.addkeyBoard();
  }
  clear() {
    this.scene.remove(playerState.playerMesh);
  }
  addkeyBoard() {
    document.onkeydown = (e: any) => {
      if (!playerState.isCrash) playerState.isMove = true;
      if (e.key === "q") {
        //切换视角
        this.type = this.type === 1 ? 2 : 1;
      } else {
        this.keyBoardEvent(e, "down");
      }
    };
    document.onkeyup = (e: any) => {
      playerState.isMove = false;
      if (e.key !== "q") this.keyBoardEvent(e, "up");
    };
  }
  keyBoardEvent(e, type) {
    if (e.keyCode == 27) {
      //esc
      document.exitPointerLock();
      return;
    }
    //TODO:
    // switch (e.keyCode) {
    //   case 38: // up
    //   case 87: // w
    //     if (type == "down") {
    //       this.forwardSpeed = -this.speed;
    //     } else {
    //       this.forwardSpeed = 0;
    //     }
    //     break;
    //   case 37: // left
    //   case 65: // a
    //     if (type == "down") {
    //       this.translateSpeed = -this.speed;
    //     } else {
    //       this.translateSpeed = 0;
    //     }
    //     break;
    //   case 40: // down
    //   case 83: // s
    //     if (type == "down") {
    //       this.forwardSpeed = this.speed;
    //     } else {
    //       this.forwardSpeed = 0;
    //     }
    //     break;
    //   case 39: // right
    //   case 68: // d
    //     if (type == "down") {
    //       this.translateSpeed = this.speed;
    //     } else {
    //       this.translateSpeed = 0;
    //     }
    //     break;
    //   case 32: // 空格
    //     this.speedVel.y = 0.1;
    //     break;
    // }
  }
  update(delta) {
    this.handleDirection();
    this.handleMove(delta);
    this.updateControls();
  }
  async handleMove(deltaTime: number) {
    // let damping = Math.exp(-4 * deltaTime) - 1; //阻尼
    // const speedDelta = deltaTime * 20;
    // const direction = this.forwardDirection.clone();
    // const forwardSpeed = direction.multiplyScalar(
    //   this.forwardSpeed * speedDelta
    // );

    // const translateSpeed = this.translateDirection.multiplyScalar(
    //   this.translateSpeed * speedDelta
    // );
    // const mulitySpeed = forwardSpeed.add(translateSpeed);
    // this.speedVel.x = mulitySpeed.x;
    // this.speedVel.z = mulitySpeed.z;
    // this.speedVel.addScaledVector(this.speedVel, damping);

    // //刚体移动
    // const position = new THREE.Vector3();
    // position.copy(playerState.playerBody.position);
    // position.add(this.speedVel);
    // //方式一：和一些刚体行为冲突
    // // playerState.playerBody.position.copy(position);
    // //方式二：应用一些刚体的惯性和加速度
    // playerState.playerBody.applyImpulse(
    //   this.speedVel.normalize(),
    //   playerState.playerBody.position
    // );
    // //方式三：

    // // if (playerState.isCrash) this.speedVel.negate(); //检测到碰撞采用反方向
    // // playerState.playerMesh.position.add(this.speedVel);

    // this.check();
  }
  check() {
    // if (playerState.playerMesh.position.y < -100) {
    //   //恢复初始位置
    //   this.speedVel.set(0, this.offsetY, 0);
    //   playerState.playerMesh.position.add(this.speedVel);
    // }
    // if (playerState.playerBody.position.y < -100) {
    //   //恢复初始位置
    //   this.speedVel.set(0, this.offsetY, 0);
    //   //刚体移动
    //   const position = new THREE.Vector3();
    //   position.copy(playerState.playerBody.position);
    //   position.add(this.speedVel);
    //   playerState.playerBody.position.copy(position);
    //   playerState.playerMesh.position.add(this.speedVel);
    // }
  }
  handleDirection() {
    // phi是方位面（水平面）内的角度，范围0~360度，φ
    // theta是俯仰面（竖直面）内的角度，范围0~180度，符号θ
    // const camera = this.camera;
    // const yRadian = camera.rotation.y;
    // this.forwardDirection.x = Math.sin(yRadian);
    // this.forwardDirection.z = Math.cos(yRadian);
    // this.translateDirection.x = Math.sin(yRadian + Math.PI * 0.5);
    // this.translateDirection.z = Math.cos(yRadian + Math.PI * 0.5);
  }
  updateControls() {
    // const position = playerState.playerMesh.position.clone();
    // const direction = this.forwardDirection.clone();
    // playerState.direction = this.forwardDirection.clone();
    // const offsetPosition = new THREE.Vector3();
    // if (this.type === 1) {
    //   offsetPosition.copy(position);
    // } else if (this.type === 2) {
    //   offsetPosition.copy(position.sub(direction.multiplyScalar(-1)));
    //   offsetPosition.y += 1;
    // }

    // this.camera.position.copy(offsetPosition);
  }
}
